package com.ash.android_adk_demo

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hc.schedule.model.AudioTask
import com.hc.schedule.model.TaskStatus

interface OnTaskClickListener {
    fun onTaskClick(task: AudioTask)
}


class TasksAdapter(private val tasks: Array<AudioTask>, private val listener: OnTaskClickListener? = null) : RecyclerView.Adapter<TasksAdapter.TaskViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.item_task, parent, false)
        return TaskViewHolder(view)
    }

    override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
        holder.bind(tasks[position])
    }

    override fun getItemCount(): Int = tasks.size


    inner class TaskViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val ivTaskThumbnail: ImageView = itemView.findViewById(R.id.iv_task_thumbnail)
        private val tvTaskName: TextView = itemView.findViewById(R.id.tv_task_name)
        private val tvTaskDuration: TextView = itemView.findViewById(R.id.tv_task_duration)
        private val tvTaskStatus: TextView = itemView.findViewById(R.id.tv_task_status)

        fun bind(task: AudioTask) {
            // 加载任务缩略图
            Glide.with(itemView.context)
                .load(task.imgUrl)
                .placeholder(android.R.drawable.ic_menu_gallery)
                .error(android.R.drawable.ic_menu_gallery)
                .into(ivTaskThumbnail)

            // 设置任务名称
            tvTaskName.text = task.name

            // 设置任务时长
            val minutes = task.duration / 60
            val seconds = task.duration % 60
            tvTaskDuration.text = "时长: ${minutes}分${seconds}秒"

            // 设置任务状态
            when (task.status) {
                TaskStatus.Finished -> {
                    tvTaskStatus.text = "已完成"
                    tvTaskStatus.setBackgroundColor(ContextCompat.getColor(itemView.context, android.R.color.holo_green_light))
                }
                TaskStatus.NotFinished -> {
                    tvTaskStatus.text = "未完成"
                    tvTaskStatus.setBackgroundColor(ContextCompat.getColor(itemView.context, android.R.color.holo_orange_light))
                }
                TaskStatus.NoSubscribed -> {
                    tvTaskStatus.text = "未订阅"
                    tvTaskStatus.setBackgroundColor(ContextCompat.getColor(itemView.context, android.R.color.holo_red_light))
                }
            }

            // 设置点击事件
            itemView.setOnClickListener {
                if(task.status == TaskStatus.Finished){
                    // 播放已完成的任务音频

                    itemView.context.showToast( "播放已完成的任务音频")
                }else if(task.status == TaskStatus.NotFinished){
                    // 开始音频任务
                    if (listener == null){
                        itemView.context.showToast( "不是当前天计划,不可播放",)
                    }
                    listener?.onTaskClick(task)
                }else if (task.status == TaskStatus.NoSubscribed){
                    itemView.context.showToast( "没有订阅")

                }
            }
        }
    }
}